package hzn;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class AccessFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";//在路由之前过滤，post路由后再过滤
    }

    @Override
    public int filterOrder() {
        return 0;//如果存在多个过滤器，由数字大小决定顺序，越小优先级越高.
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext ctx=RequestContext.getCurrentContext();
        HttpServletRequest req=ctx.getRequest();
        String path=req.getServletPath();
        String id=path.substring(path.lastIndexOf("/")+1);

        System.out.println("path="+path+",id="+id);
        if(id==null||Integer.parseInt(id)>10){
            ctx.setSendZuulResponse(false);//拒绝提供路由功能
        }
        return null;
    }
}
